/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.consultorio.controle;

import br.com.consultorio.bean.Convenio;
import br.com.consultorio.bean.MaterialUso;
import br.com.consultorio.dao.ConvenioDao;
import br.com.consultorio.dao.ConvenioDaoImp;
import br.com.consultorio.dao.MaterialUsoDao;
import br.com.consultorio.dao.MaterialUsoDaoImp;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Thiago
 */
@WebServlet(name = "ServletMateriais", urlPatterns = {"/ServletMateriais"})
public class ServletMateriais extends HttpServlet {

    private HttpServletRequest request;
    private HttpServletResponse response;
    private RequestDispatcher rd;
    private MaterialUsoDao materialUsoDao;

    protected void processRequest()
            throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        switch (cmd) {
            case "cadMat":
                carregaMateriais();
                break;
            case "salvar":
                salvar();
                break;
            case "pesqNome":
                pesquisaNome();
                break;
        }
        rd.forward(request, response);
    }

    private void salvar() {
        materialUsoDao = new MaterialUsoDaoImp();
        MaterialUso materialUso = carregaMateriais();
        try {
            materialUsoDao.salvar(materialUso);
            //manda e-mail com dados do usuário
            rd = request.getRequestDispatcher("admin/pesqMateriais.jsp");
        } catch (Exception e) {
            System.out.println("erro ao salvar materiaisUso " + e.getMessage());
        }
    }

    private void pesquisaNome() {
        materialUsoDao = new MaterialUsoDaoImp();
        try {
            List<MaterialUso> materiais = materialUsoDao.pesquisaPorNome(request.getParameter("nome"));
            request.setAttribute("materiais", materiais);
            rd = request.getRequestDispatcher("admin/pesqMateriais.jsp");

        } catch (SQLException ex) {
            Logger.getLogger(ServletMateriais.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ServletMateriais.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private MaterialUso carregaMateriais() {
        MaterialUso materialUso = new MaterialUso();

        materialUso.setNome(request.getParameter("nome"));
        materialUso.setDescricao(request.getParameter("descricao"));
        materialUso.setValor(Double.parseDouble(request.getParameter("valor")));

        return materialUso;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }
}
